const verifyStatusCode = {
    '1000': 'ok',                      //通过校验
    '1001': '账号需要5-12个字符',      //account长度不合法
    '1002': '账号重复',                //账号重复
    '1003': '密码需要5-12个字符',       //pass长度不合法
    '1004': '两次密码不相同',             //两次密码不相同
    '1005': '邮箱长度不正确',            //邮箱长度不合法
    '1006': '邮箱不合法',                 //邮箱格式不合法
    '1007': '个人简介限制30字符',          //个人简介长度不合法
    '1008': '账号不存在',                 //账号不存在
    '1009': '密码不正确',                    //密码与账号不匹配
    '1010': '长度需要3-25个字符',         //长度不正确
    '1011': '密钥不正确',                //管理员密钥不正确
    '1012': 'topic标题最多20个字符',      //topic标题过长
    '1013': 'topic描述最多20个字符',       //topic描述过长
    '1014': '必须输入标题'                  //标题长度等于0
}

const { ReplyError } = require('redis');
const userDao = require('../../database/redis/userDao');

let asyncVerifyAccount = async function (account) {

    //校验长度。。
    let length = account.length;
    if (length < 5 || length > 12) {
        return '1001';
    }

    //在redis校验查重
    if (await userDao.asyncisExistAccount(account)) {
        return '1002';
    }

    return '1000';
}

let verifyLength = function (account) {
    //校验长度。。
    let length = account.length;
    if (length < 3 || length > 25) {
        return '1010';
    }

    return '1000';
}


let verifyPass = function (pass1, pass2) {

    //校验密码长度
    let length = pass1.length;
    if (length < 5 || length > 12) {
        return '1003'
    }

    //校验相同
    if (pass1 !== pass2) {
        return '1004'
    }

    return '1000'
}

let verifyEmail = function (email) {

    //校验邮箱长度
    let length = email.length;
    if (length < 5 || length > 20) {
        return '1005';
    }

    //正则校验邮箱
    var re = /^\w+@[a-z0-9]+\.[a-z]{2,4}$/;
    if (!re.test(email)) {
        return '1006';
    }

    return '1000';
}

let verifyResume = function (resume) {

    let length = resume.length;
    if (length > 30) {
        return '1007';
    }

    return '1000';
}

let asyncVerifyAccountPassword = async function (account, password) {

    let exists = await userDao.asyncisExistAccount(account);
    if (!exists) {
        return '1008';
    }

    let userId = await userDao.asyncGetIdByAccount(account);
    let userData = await userDao.asyncGetDataById(userId);
    let userPassword = userData.password;
    if (userPassword !== userDao.hashPassword(password)) {
        return '1009';
    }

    return '1000';
}


//管理员密钥校验器
function verifyAdminKey(adminSelect, adminKey) {

    if (adminSelect == 1 && adminKey !== 'eggshell.xyz') {
        return '1011'
    }

    return '1000';
}

//register的校验器,而且负责向模板引擎发送错误消息
async function asyncRegisterVerify(req, account, password, re_password, email, resume, adminSelect, adminKey) {
    let status = true;

    let accountStatusCode = await asyncVerifyAccount(account);
    //状态码为1000通过校验
    if (accountStatusCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[accountStatusCode],
            type: 'tips-account'
        });
    }

    let passStatusCode = verifyPass(password, re_password);
    if (passStatusCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[passStatusCode],
            type: 'tips-pass'
        });
    };

    let emailStatusCode = verifyEmail(email);
    if (emailStatusCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[emailStatusCode],
            type: 'tips-email'
        })
    }

    let resumeSatausCode = verifyResume(resume);
    if (resumeSatausCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[resumeSatausCode],
            type: 'tips-resume'
        })
    }

    let adminKeyStausCode = verifyAdminKey(adminSelect, adminKey);
    if (adminKeyStausCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[adminKeyStausCode],
            type: 'tips-adminKey'
        })
    }

    return status;
}

//topic标题校验器
function titleVerify(title) {
    let length = title.length;
    if (length > 20) {
        return '1012';
    }

    if (length <= 0) {
        return '1014';
    }

    return '1000';
}

//topic描述校验器
function discribeVerify(title) {
    let length = title.length;
    if (length > 20) {
        return '1013';
    }
    return '1000';
}



//登录页校验器
async function asyncLoginVerify(req, account, password) {
    let status = true;

    password = password || '';

    let APstatusCode = await asyncVerifyAccountPassword(account, password);
    if (APstatusCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[APstatusCode],
            type: 'tips-password'
        })
    }
    return status;
}

//topic校验器
function topicVerify(req, title, discribe) {
    let status = true;

    let titleStatusCode = titleVerify(title);
    if (titleStatusCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[titleStatusCode],
            type: 'tips-tipic-title'
        })
    }

    let discribeStatusCode = discribeVerify(discribe);
    if (discribeStatusCode != '1000') {
        status = false;
        req.addMessage({
            msg: verifyStatusCode[discribeStatusCode],
            type: 'tips-tipic-discribe'
        })
    }

    return status;
}

//topic_comment的校验器
function topicCommentVerify(req,title,content){
    let status = true;

    let titleStatusCode = verifyLength(title);
    if(titleStatusCode != '1000'){
        status = false;
        req.addMessage({
            msg:verifyStatusCode[titleStatusCode],
            type:'tips-tipic-content-title'
        })
    }
    
    let contentStatusCode = verifyLength(content);
    if(contentStatusCode != '1000'){
        status = false;
        req.addMessage({
            msg:verifyStatusCode[contentStatusCode],
            type:'tips-tipic-content-content'
        })
    }
    
    return status;
}


module.exports = {
    asyncRegisterVerify,
    asyncLoginVerify,
    topicVerify,
    topicCommentVerify
}